//
//  YZMTrendPacket.m
//  YZMicroStock
//
//  Created by wangminhong on 16/4/12.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMTrendPacket.h"
#import "YZMTrendItem.h"

@implementation YZMTrendPoint

@end

@implementation YZMTrendPacket{
    
    NSArray *_trendItems;
}

- (void)setOriginItems: (NSArray *)items
{
    _trendItems = [items copy];
}

- (void)updateFromItems: (NSArray *)items
{
    NSMutableArray *array = _trendItems.mutableCopy;
    for (YZMTrendItem *item in items) {
        NSInteger count = array.count;
        for (NSInteger i = count - 1; i >= 0; i--) {
            YZMTrendItem *ori = array[i];
            if (ori.time < item.time) {
                [array insertObject: item atIndex: i+1];
                break;
            }
            else if (ori.time == item.time)
            {
                [array replaceObjectAtIndex: i withObject: item];
                break;
            }
        }
    }
    _trendItems = [array copy];
}

- (void)_updatePriceOffset
{
    if (_closePrice == 0) {
        _maxOffset = 0.1;
        return;
    }
    else
    {
        _maxOffset = 0.0;
    }
    
    for (YZMTrendItem *item in _trendItems) {
        if (item.price > 0) {
            double tempFloat = 1.0 * fabs(item.price - _closePrice) / _closePrice;
            if (tempFloat > _maxOffset) {
                _maxOffset = tempFloat;
            }
        }
    }
    if (_highPrice > DBL_EPSILON) {
        double updateOffset = fabs(_highPrice-_closePrice) / _closePrice;
        _maxOffset = _maxOffset > updateOffset ? _maxOffset : updateOffset;
    }
    if (_lowPrice > DBL_EPSILON) {
        double updateOffset = fabs(_lowPrice-_closePrice) / _closePrice;
        _maxOffset = _maxOffset > updateOffset ? _maxOffset : updateOffset;
    }
    if (_maxOffset < DBL_EPSILON) {
        _maxOffset = 0.1;
    }
}

- (void)_updateVolumeMax
{
    _maxVolume = 0;
    for (YZMTrendItem *item in _trendItems) {
        if (item.volume > _maxVolume) {
            _maxVolume = item.volume;
        }
    }
}


- (NSArray *)trendDrawPoints
{
    if (_trendItems.count == 0 || _closePrice < 1) {
        _maxOffset = 0.1;
        _maxVolume = 0;
        return nil;
    }
    [self _updatePriceOffset];
    [self _updateVolumeMax];
    
    NSMutableArray *resArray = [NSMutableArray array];
    
    YZMTrendItem *preItem = nil;
    for (YZMTrendItem *item in _trendItems) {
        YZMTrendPoint *point = [[YZMTrendPoint alloc] init];
        if (_closePrice > DBL_EPSILON) {
            point.price = (_closePrice - item.price) / _closePrice / _maxOffset;
        }
        if (_maxVolume > DBL_EPSILON) {
            point.volume = item.volume * 1.0  / _maxVolume;
        }
        
        
        if (preItem) {
            point.rise = (item.price >= preItem.price);
        }
        else
        {
            point.rise = (item.price >= _closePrice);
        }
        
        preItem = item;
        
        [resArray addObject: point];
    }
    return [resArray copy];
}

- (YZMTrendItem *)lastItem
{
    return [_trendItems lastObject];
}


- (YZMTrendItem *)itemOfIndex: (NSInteger)index
{
    if (index > -1 && index < _trendItems.count) {
        return _trendItems[index];
    }
    return nil;
}

- (void)clearData
{
    _closePrice = 0;
    _maxOffset = 0;
    _maxVolume = 0;
    _lowPrice = 0;
    _highPrice = 0;
    _trendItems = nil;
    
}

@end
